package com.heima.media.task;

import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ArticleDto;
import com.heima.media.entity.WmNews;
import com.heima.media.feign.ArticleFeign;
import com.heima.media.service.IAuditService;
import com.heima.media.service.IWmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class ArticleTask {
    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private IWmNewsService wmNewsService;
    @Autowired
    private IAuditService auditService;


    @Async
    public void receiveMesasge() throws Exception {
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("news-dqueue");

        while (true) {
            Object result = blockingDeque.poll(30, TimeUnit.SECONDS);
            if (result != null) {
                log.info("====================获取到发布消息====================");
                Integer newsId = Integer.valueOf(result.toString());
                //            获取到消息后发布文章
                WmNews wmNews = wmNewsService.getById(newsId);
                Long articleId = auditService.saveArticle(wmNews);//把auditService中方法做出public 并且在接口中添加
                wmNews.setArticleId(articleId);
                wmNews.setStatus(9);
                wmNewsService.updateById(wmNews);
                log.info("====================延迟发布任务成功====================");
            }
        }
    }

}
